package cn.com.b2office.struts2.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;

import cn.com.b2office.domain.Article;
import cn.com.b2office.domain.Item;
import cn.com.b2office.domain.SpecialItem;
import cn.com.b2office.service.ArticleService;
import cn.com.b2office.service.ItemService;

import com.opensymphony.xwork2.ActionSupport;

public class ArticleShowAction extends ActionSupport implements
		ServletRequestAware {

	private static final long serialVersionUID = 4390071049096147203L;

	// 存放最近浏览的cookie名称
	public static final String COOKIE_NAME = "historyitems";

	private static final Log logger = LogFactory
			.getLog(ArticleShowAction.class);

	private HttpServletRequest request = null;

	private int articleId = 0;

	private ArticleService articleService;

	private ItemService itemService = null;

	private Article article;

	private Article nextArticle;

	private List<Item> history = null;

	/* 热卖产品 */
	private List<List> hotItems = null;

	/* 特价产品 */
	private List<List> cheapItems = null;

	public String execute() throws Exception {

		// 最近浏览
		Cookie c = getCookie();
		List<String> historyItemIdList = getHistoryRecords(c);
		history = itemService.findItemsByIterate(historyItemIdList);
		// 热卖商品
		List<SpecialItem> hotSpecialItems = itemService.findSpecialItems(2, 0,
				10);
		List<Item> items2 = itemService
				.findItems(getItemIdList(hotSpecialItems));
		hotItems = buildSpecialItem(hotSpecialItems, items2);
		// 特价商品
		List<SpecialItem> cheapSpecialItems = itemService.findSpecialItems(3,
				0, 10);
		List<Item> items3 = itemService
				.findItems(getItemIdList(cheapSpecialItems));
		cheapItems = buildSpecialItem(cheapSpecialItems, items3);

		article = articleService.getArticle(articleId);
		nextArticle = articleService.getNextArticle(articleId);

		return SUCCESS;
	}

	private List<String> getItemIdList(List<SpecialItem> items) {
		List<String> itemIdList = new ArrayList<String>();
		if (items == null || items.isEmpty())
			return itemIdList;
		for (SpecialItem si : items) {
			itemIdList.add(si.getItemId());
		}
		return itemIdList;
	}

	private List<List> buildSpecialItem(List<SpecialItem> specialItems,
			List<Item> items) {
		if (items == null || items.isEmpty())
			return null;
		List<List> results = new ArrayList<List>();
		Map<String, SpecialItem> map = new HashMap<String, SpecialItem>();
		for (SpecialItem si : specialItems) {
			map.put(si.getItemId(), si);
		}
		for (Item item : items) {
			SpecialItem st = map.get(item.getItem_no());
			if (st == null)
				continue;
			List l = new ArrayList();
			l.add(item);
			l.add(st);
			results.add(l);
		}
		return results;
	}

	/**
	 * 获取最近浏览历史记录的cookie中
	 * 
	 * @return
	 */
	private Cookie getCookie() {
		Cookie[] cookies = request.getCookies();
		if (cookies == null || cookies.length == 0) {
			return null;
		}
		for (Cookie c : cookies) {
			if (c.getName().equals(COOKIE_NAME)) {
				return c;
			}
		}
		return null;
	}

	/**
	 * 获取最近浏览记录
	 * 
	 * @return
	 */
	private List<String> getHistoryRecords(Cookie c) {
		List<String> list = new ArrayList<String>();
		if (c == null || c.getValue() == null || c.getValue().trim().equals(""))
			return list;
		String[] values = c.getValue().split("&");
		for (int i = 0; i < values.length; i++) {
			list.add(values[i]);
		}
		return list;

	}

	public void setArticleId(int articleId) {
		this.articleId = articleId;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	public Article getArticle() {
		return article;
	}

	public Article getNextArticle() {
		return nextArticle;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public List<Item> getHistory() {
		return history;
	}

	public List<List> getCheapItems() {
		return cheapItems;
	}

	public List<List> getHotItems() {
		return hotItems;
	}

}
